package j.util.jdbc.simple.pagination;

import j.data.PagedData;

import javax.sql.DataSource;
import java.util.List;

/**
 * 依赖于spring的JDBC分页
 */
public interface Pagination {
	static final int ORACLE_PROVIDER = 1;
	static final int MYSQL_PROVIDER = 2;
	static final int MSSQL_PROVIDER = 3;
	/**
	 * oracle的分页模板
	 * %1$s => 查询语句
	 * %2$d => endRow
	 * %3$d => startRow
	 */
	static final String ORACLE_PAGING_TEMPLATE = "SELECT * FROM (" +
			"SELECT ROWNUM rn,TBL_ORACLE_PAGINATION_.* FROM(" +
			"	%1$s) TBL_ORACLE_PAGINATION_ WHERE ROWNUM<=%2$d) WHERE rn>=%3$d";
	
	/**
	 * MySql的分页模板
	 * %1$s => 查询语句
	 * %2$d => offset 记录行的偏移量，以0为基数
	 * %3$d => rows 页大小
	 */
	static final String MYSQL_PAGING_TEMPLATE = "%1$s LIMIT %2$d,%3$d";
	//select * from (select row_number()over(order by order_template) as rn_,mssql_page_t1.* from (
	//select top 10 *,0 order_template from Customers order by CustomerID) mssql_page_t1) mssql_page_t2 where mssql_page_t2.rn_>=1
	static final String MSSQL_PAGING_TEMPLATE = "SELECT * FROM (" +
			"SELECT ROW_NUMBER()OVER(ORDER BY MSSQL_PAGE_T1.ORDER_TEMPLATE) AS RN_,MSSQL_PAGE_T1.* FROM (" +
			"%1$s) MSSQL_PAGE_T1) MSSQL_PAGE_T2 WHERE MSSQL_PAGE_T2.RN_>=%2$d";
	
	/**
	 * 全独立的jdbc语句来查询
	 * @param dataSource
	 * @param sqlFetchRows
	 * @param args
	 * @param pageNo
	 * @param pageSize
	 * @return
	 */
	public <E> PagedData<List<E>> fetchPage(
			final DataSource dataSource,
			final Class<E> requiredType,
			final String sqlFetchRows,
			final Object[] args,
			final int pageNo,
			final int pageSize
	);
}
